﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project.WebAPI.Controllers
{
    /// <summary>
    /// 手机验证码服务
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ValidateCodeController : ControllerBase
    {
        private readonly IConfiguration configuration;

        public ValidateCodeController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet("{phone}")]
        public ActionResult GetCode(string phone)
        {
            var random = new Random();
            var code = random.Next(100000, 999999).ToString();
            // 读取配置文件中的短信模板
            var msg = configuration["ValidateCode:Template"].Replace("##", code);

            // 此处发送短信服务

            HttpContext.Session.SetString(phone, code);  // session 保存验证码
            
            return NoContent();
        }
    }
}
